package com.liu.mybatisPlus.wm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.mybatisPlus.wm.entity.base.WmRivenItem;
import com.liu.mybatisPlus.wm.entity.base.WmRivenItemKeyWord;
import com.liu.mybatisPlus.wm.mapper.WmRivenItemKeyWordMapper;
import com.liu.mybatisPlus.wm.service.WmRivenItemKeyWordService;
import com.liu.mybatisPlus.wm.service.WmRivenItemService;
import com.liu.qqRobot.wm.entity.dto.WmRivenItemDTO;
import com.liu.qqRobot.wm.entity.qo.AddWmRivenItemKeyWordQO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiuChengLong
 * @since 2025-04-01
 */
@Service
public class WmRivenItemKeyWordServiceImpl extends ServiceImpl<WmRivenItemKeyWordMapper, WmRivenItemKeyWord> implements WmRivenItemKeyWordService {

    @Resource
    private WmRivenItemService wmRivenItemService;

    @Override
    public List<WmRivenItemDTO> keyWordGetRiven() {
        List<WmRivenItemKeyWord> wmRivenItemKeyWords = super.list();
        List<String> rivenIdList = wmRivenItemKeyWords.stream().map(WmRivenItemKeyWord::getId).distinct().collect(Collectors.toList());
        Map<String, String> keyWordMap = wmRivenItemKeyWords.stream().collect(Collectors.toMap(WmRivenItemKeyWord::getKeyWord, WmRivenItemKeyWord::getId));
        QueryWrapper<WmRivenItem> wmRivenItemQueryWrapper = new QueryWrapper<>();
        wmRivenItemQueryWrapper.in("id", rivenIdList);
        List<WmRivenItem> wmRivenItems = wmRivenItemService.list();
        Map<String, WmRivenItem> wmRivenItemMap = wmRivenItems.stream().collect(Collectors.toMap(WmRivenItem::getId, w -> w));
        List<WmRivenItemDTO> wmRivenItemDTOS = new ArrayList<>();
        for (Map.Entry<String, String> entry : keyWordMap.entrySet()) {
            String keyWord = entry.getKey();
            String rivenId = entry.getValue();
            WmRivenItem wmRivenItem = wmRivenItemMap.get(rivenId);
            WmRivenItemDTO wmRivenItemDTO = BeanUtil.copyProperties(wmRivenItem, WmRivenItemDTO.class);
            wmRivenItemDTO.setKeyWord(keyWord);
            wmRivenItemDTOS.add(wmRivenItemDTO);
        }
        return wmRivenItemDTOS;
    }

    @Override
    public void add(List<AddWmRivenItemKeyWordQO> qoList) {
        // 校验关键词是否存在
        List<WmRivenItemKeyWord> insertList = new ArrayList<>();
        for (AddWmRivenItemKeyWordQO qo : qoList) {
            WmRivenItem wmRivenItem = wmRivenItemService.getById(qo.getRivenItemId());
            if (wmRivenItem == null) {
                continue;
            }
            QueryWrapper<WmRivenItemKeyWord> wmRivenItemKeyWordQueryWrapper = new QueryWrapper<>();
            wmRivenItemKeyWordQueryWrapper.eq("key_word", qo.getKeyWord());
            WmRivenItemKeyWord wmRivenItemKeyWord = super.getOne(wmRivenItemKeyWordQueryWrapper);
            if (wmRivenItemKeyWord != null) {
                continue;
            }
            wmRivenItemKeyWord = new WmRivenItemKeyWord();
            wmRivenItemKeyWord.setKeyWord(qo.getKeyWord());
            wmRivenItemKeyWord.setId(qo.getRivenItemId());
            insertList.add(wmRivenItemKeyWord);
        }
        super.saveBatch(insertList);
    }

}
